package cn.itcast.dao;

import cn.itcast.pojo.Menu;
import cn.itcast.vo.MenuListVo;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;


import java.util.List;

public interface MenuDao {


    /**
     * 分页查询菜单列表
     * @return
     * @param queryString
     */
    Page<Menu> findPage(String queryString);


    /**
     * 通过父菜单的id查询字菜单列表
     * @param id
     * @return
     */
    List<Menu> findByParentId(Integer id);



    /**
     * 新增菜单
     * @param menu 新增信息
     * @return
     */
    int handleAdd(Menu menu);



    //将当前菜单的权限添加到t_menu_permission表中
    int addMenuAndPermission(@Param("menu_id") Integer id, @Param("permission_id") Integer permissionId);


    /**
     * 根据id查询单条菜单信息
     * @param id
     * @return
     */
    Menu findById(Integer id);




    /**
     * 根据id查询单条菜单的权限信息
     * @param id
     * @return
     */
    List<Integer> handleUpdatePermissions(Integer id);


    /**
     * 删除当前id菜单的所有权限
     * @param id
     * @return
     */
    int deletePermission(Integer id);



    /**
     * 编辑一级菜单/编辑二级菜单
     * @param menu  菜单的基本数据
     * @return
     */
    int handleEdit(Menu menu);


    /**
     * 根据id删除菜单
     * @param id  菜单的基本数据
     * @return
     */
    int handleDelete(Integer id);


    /**
     * 删除权限中间表的数据
     * @param id
     * @return
     */
    int handleDeletePermission(Integer id);




    @Select("SELECT DISTINCT * FROM t_menu m,t_user u,t_user_role ur,t_role r,t_role_menu rm WHERE u.id=ur.user_id AND ur.role_id=r.id AND r.id=rm.role_id AND rm.menu_id=m.id AND m.`level`=1 AND u.id=#{userId} order by priority asc")
    @Results({
            @Result(column = "name",property = "title"),
            @Result(column = "linkUrl",property = "linkUrl"),
            @Result(column = "icon",property = "icon"),
            @Result(
                    property = "children",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "cn.itcast.dao.MenuDao.findMenuVoByParentMenu")
            )
    })
    List<MenuListVo> findMenuByUserId(Integer userId);

    @Select("SELECT * FROM t_menu where parentMenuId=#{id} order by priority asc")
    @Results({
            @Result(column = "name",property = "title"),
            @Result(column = "linkUrl",property = "linkUrl"),
            @Result(column = "icon",property = "icon")
    })
    List<MenuListVo> findMenuVoByParentMenu(Integer Id);


    List<String> findMenuNameByUserId(Integer userId);

}
